<template>
	<view class="anmed">
		<view class="my-top">
			<image class="header" src="https://xcx.xiaombt.com/img/img/header.png" mode=""
				:style="{ height: navbarHeight + 'px' }">
			</image>
			<view class="nav">
				<u-navbar title="上传内容" :autoBack='true' bgColor="transparent" leftIconColor='#6C5D47'
					titleStyle='color:#6C5D47' placeholder>
				</u-navbar>
			</view>
		</view>
		<view class="main" :style="{ marginTop: viewHeight + 'px' }">

			<!-- 	<view class="main-item" >
				<view class="title">
					<image src="https://xcx.xiaombt.com/img/img/line.png" mode=""></image>
					<text>上传封面</text>
				</view>
				<view class="con-img">
					<u-upload :fileList="fileList5" @afterRead="afterRead" @delete="deletePic" name="5" multiple
						:maxCount="1">
						<image src="https://xcx.xiaombt.com/img/img/up.png" mode="widthFix" style="width: 200rpx;height: 200rpx;"></image>
					</u-upload>
				</view>
			</view> -->
			<view class="main-item">
				<view class="title">
					<image src="https://xcx.xiaombt.com/img/img/line.png" mode=""></image>
					<text>上传图片</text>
				</view>
				<view class="con-img">
					<u-upload :fileList="fileList6" @afterRead="afterRead" @delete="deletePic" name="6" multiple
						:maxCount="3">
						<image src="https://xcx.xiaombt.com/img/img/up.png" mode="widthFix"
							style="width: 200rpx;height: 200rpx;"></image>
					</u-upload>
				</view>
			</view>
			<view class="main-item">
				<view class="title">
					<image src="https://xcx.xiaombt.com/img/img/line.png" mode=""></image>
					<text>标 题</text>
				</view>
				<view class="con-input">
					<u--input v-model="title" placeholder="请输入内容"></u--input>
				</view>
			</view>
			<view class="main-item">
				<view class="title">
					<image src="https://xcx.xiaombt.com/img/img/line.png" mode=""></image>
					<text>副标题</text>
				</view>
				<view class="con-input">
					<u--input v-model="title2" placeholder="请输入内容"></u--input>
				</view>
			</view>
	
			<view class="main-item" @click="()=>show = !show">
				<view class="title">
					<image src="https://xcx.xiaombt.com/img/img/line.png" mode=""></image>
					<text>文章类型</text>
				</view>
				<view class="con-input">
					<u--input v-model="type.name" placeholder="请选择文章类型"></u--input>
				</view>
			</view>
			<view class="main-item">
				<view class="title">
					<image src="https://xcx.xiaombt.com/img/img/line.png" mode=""></image>
					<text>文章来源</text>
				</view>
				<view class="con-input">
					<u--input v-model="laiyuan" placeholder="请输入内容"></u--input>
				</view>
			</view>
			<view class="main-item">
				<view class="title">
					<image src="https://xcx.xiaombt.com/img/img/line.png" mode=""></image>
					<text>文章内容</text>
				</view>
				<view class="con-text">
					<u--textarea v-model="content" placeholder="请输入内容"></u--textarea>
				</view>
			</view>

			<view class="main-button" @click="tijiao">
				提 交
			</view>
			<u-picker :show="show" :columns="columns" @confirm="confirm" keyName="title"></u-picker>
		</view>

	</view>
</template>

<script>
	import {
		zixunUp,
		consultType
	} from '@/api/index.js'
	import {
		ENV
	} from '.env.js'
	export default {
		data() {
			return {
				show:false,
				viewHeight: null,
				navbarHeight: null,
				laiyuan: '',
				content: '',
				fileList5: [],
				fileList6: [],
				ENV: null,
				columns: [],
				type:{
					name:'',
					id:''
				},
				title2:'',
				title:''
			};
		},
		async onLoad() {
			this.ENV = ENV
			const query = uni.createSelectorQuery().in(this);
			query.select('.nav').boundingClientRect(data => {
				if (data) {
					this.navbarHeight = data.height;
				}
			}).exec();
			query.select('.my-top').boundingClientRect(data => {
				if (data) {
					this.viewHeight = data.height;
				}
			}).exec();
			consultType({}).then(res => {
				this.columns = [res.rst]
			})
		},
		methods: {
			tijiao(){
				console.log(this.fileList6)
				if(this.laiyuan == '' || this.content== ''|| this.title2 == ''|| this.title == ''|| this.type.name == ''){
					uni.showModal({
						content: '请认真填写！！',
						showCancel: false
					});
					return false
				}
				zixunUp({
					laiyuan:this.laiyuan,
					content:this.content,
					title2:this.title2,
					title:this.title,
					type:this.type.id,
					list_img:this.fileList6.map(r=>r.img)
				}).then(res=>{
					if (res.errno ==='0') {
						uni.showToast({
							title: '上传成功！！',
							icon: 'none'
						});
						setTimeout(()=>{
							uni.switchTab({
							  url:'/pages/my/index'
							})
						},1500)
					} else {
						uni.showToast({
							title: res.errmsg,
							icon: 'none'
						});
					}
				})
			},
			confirm(e){
				this.type.id = e.value[0].id
				this.type.name = e.value[0].title
				this.show = !this.show
			},
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
				console.log(result)
					let item = this[`fileList${event.name}`][fileListLen]
		
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: this.ENV.BASE_API + result,
						img: result

					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: ENV.BASE_API + 'api.php?m=index.uploads',
						filePath: url,
						header: {
							'Content-Type': 'multipart/form-data',
						},
						name: 'file_upload',
						success: (res) => {
							uni.hideLoading();
								
							setTimeout(() => {
								resolve(JSON.parse(res.data).rst)
							}, 1000)

						},
						fail: (err) => {
							uni.hideLoading();
							uni.showModal({
								content: err.errMsg,
								showCancel: false
							});
						},
					});
				})
			},
		}
	};
</script>


<style lang="scss" scoped>
	/deep/.u-textarea {
		background: none !important;
		border: none !important;

	}

	/deep/.u-album__row__wrapper {
		margin-right: 14rpx !important;

		image {
			width: 212rpx !important;
			height: 206rpx !important;
		}
	}

	.main {
		padding-bottom: 50rpx;

		padding-top: 20rpx;


		.main-button {
			width: 676rpx;
			height: 96rpx;
			background-image: url('https://xcx.xiaombt.com/img/img/tijiao.png');
			background-size: 100% 100%;
			text-align: center;
			line-height: 96rpx;
			color: #fff;
			margin: 48rpx auto;
			font-size: 32rpx;
			font-weight: 550;
		}

		.main-item {
			width: 676rpx;
			margin: auto;
			margin-top: 20rpx;

			.title {
				display: flex;
				align-items: center;
				margin: 32rpx 0;

				image {
					width: 8rpx;
					height: 41rpx;
					display: inline-block;
					margin-right: 20rpx;
				}

				text {
					color: rgb(108, 93, 71);
					font-family: 思源黑体;
					font-size: 32rpx;
					font-weight: 500;

				}
			}



			.con-text {
				width: 100%;
				background-image: url('https://xcx.xiaombt.com/img/img/detail.png');
				background-size: 100% 100%;

			}

			.con-input {
				width: 100%;
				background-image: url('https://xcx.xiaombt.com/img/img/input.png');
				background-size: 100% 100%;
				border: 1rpx solid #6C5D47;
			}
		}
	}








	.my-top {
		width: 100%;
		background-color: #F2EFE9;
		position: fixed;
		z-index: 10000;
		top: 0;
		left: 0;

		.header {
			width: 100%;
			position: absolute;
			top: 0;
			left: 0;
		}

		.select {
			padding: 20rpx 38rpx;
			background-color: #fff;

			.center {
				width: 100%;
				height: 76rpx;
				position: relative;
				display: flex;
				align-items: center;
				padding: 20rpx 10rpx;
				box-sizing: border-box;
				justify-content: center;

				image {
					width: 100%;
					height: 100%;
					position: absolute;
					top: 0;
					left: 0;
				}
			}
		}

	}
</style>